package com.odm.communitymanage.ui.DoorSafe

import android.view.View
import com.blankj.utilcode.util.ToastUtils
import com.jeremyliao.liveeventbus.LiveEventBus
import com.odm.communitymanage.R
import com.odm.communitymanage.base.BaseFragment
import jsc.kit.wheel.base.WheelItem
import jsc.kit.wheel.dialog.ColumnWheelDialog
import jsc.kit.wheel.dialog.DateTimeWheelDialog
import kotlinx.android.synthetic.main.fragment_visitor_invite.*
import java.lang.StringBuilder
import java.text.SimpleDateFormat
import java.util.*


/**
 * @description: 来访邀请填写内容页面
 * @author: ODM
 * @date: 2020/4/12
 */
class InviteVisitorFragment : BaseFragment(){

    var addressPicker : ColumnWheelDialog<WheelItem, WheelItem, WheelItem, WheelItem, WheelItem> ?= null
    var timePicker : DateTimeWheelDialog ?= null

    lateinit var address : String
    lateinit var time :String


    override fun initViews() {
        etAddress_Visitor_Invite?.setOnClickListener {
            if(addressPicker != null){
                addressPicker?.show()
            }else {
                createAddressPicker()
            }
        }
        etStartTime_Visitor_Invite?.setOnClickListener {
            if(timePicker != null){
                timePicker?.show()
            }else {
                createTimePicker()
            }
        }


        btnPostVisitorCard.setOnClickListener {
            //假数据，发送事件:一个包含所有信息的邀请实体类
            //特殊判断
            var visitor_count : Int ?= 0
            if(etVisitorCount_Visitor_Invite.text.toString() != ""){
                visitor_count = etVisitorCount_Visitor_Invite.text?.toString()?.toInt()
            }
            val tenantName : String = etTenant_Visitor_Invite.text.toString()
            if(tenantName == ""){
                ToastUtils.showShort("请填写 受访人 姓名")
                return@setOnClickListener
            }
            val address = etAddress_Visitor_Invite.text.toString()
            if(address == ""){
                ToastUtils.showShort("请选择 受访地址")
                return@setOnClickListener
            }
            LiveEventBus.get("fake_visitor_card")
                .post(PostInvitorInfo(tenantName, address,
                    etStartTime_Visitor_Invite.text.toString(),
                    etVisitorName_Visitor_Invite.text.toString(),
                     visitor_count?:0))

            start(VisitorCardFragment())
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_visitor_invite
    }

    /**
     * 初始化 地址选择器
     */
    fun createAddressPicker(){
        addressPicker =  ColumnWheelDialog(_mActivity)
        if(addressPicker?.isShowing == false) {
            addressPicker?.show()
        }
        addressPicker?.setItems(
            initItems(" 栋"),initItems(" 号单元"),initItems(" 楼"),initItems(" 室"),null
        )
        addressPicker?.setTitle("选择拜访地址");
        addressPicker?.setTextSize(25f)
        addressPicker?.setItemVerticalSpace(60)
        addressPicker?.setCancelButton("取消", null);
        addressPicker?.setOKButton("确认"
        ) { v, item0, item1, item2, item3, item4 ->
            val sb = StringBuilder()
            //赋值给address 变量
            sb.append(item0?.showText).append(" ").append(item1?.showText)
                .append(" ").append(item2?.showText).append(" ").append(item3?.showText)
                .append(" ").append(item4?.showText ?:"")
            address = sb.toString()
            etAddress_Visitor_Invite?.text = address
            false
        }
    }


    private fun initItems(label: String): Array<WheelItem?>? {
        val items = arrayOfNulls<WheelItem>(20)
        for (i in 0..19) {
            val index = i+1
            items[i] = WheelItem( index.toString() + label)
        }
        return items
    }

    /**
     * 初始化时间选择器
     */
    private fun  createTimePicker(){
        var calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, 2019);
        calendar.set(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        //获取时间间隔
        val startDate = calendar.time;
        calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, 2021)
        val endDate = calendar.time

        timePicker =  DateTimeWheelDialog(_mActivity)
        timePicker?.setShowCount(7)
        timePicker?.setItemVerticalSpace(40)
        timePicker?.show();
        timePicker?.setTitle("选择拜访时间");
        val config = DateTimeWheelDialog.SHOW_YEAR_MONTH_DAY_HOUR_MINUTE
        timePicker?.configShowUI(config)
        timePicker?.setCancelButton("取消", null)

        timePicker?.setOKButton("确定"){
            v: View?, selectedDate: Date ->
            etStartTime_Visitor_Invite.text = SimpleDateFormat.getInstance().format(selectedDate).toString()
            false
        }
        timePicker?.setDateArea(startDate, endDate, true)
        timePicker?.updateSelectedDate( Date())
    }

    data class PostInvitorInfo(
        val visited_name : String,
        val address : String ,
        val startTime : String,
        val visitor_name : String,
        val visitors_count : Int
    )

}